home *** CD-ROM | disk | FTP | other *** search
- /* File: Windows.c */
-
- #include "AESimple.h"
-
- void SetScrollMaxima (WindowPtr wp, wiHand info, Boolean setToZero)
- {
- /* Set the maximum values for both the horizontal and vertical scrollbars */
- short maxVValue = (wp->portRect.bottom - wp->portRect.top) - ((*info)->picBounds.bottom - (*info)->picBounds.top);
- short maxHValue = (wp->portRect.right - wp->portRect.left) - ((*info)->picBounds.right - (*info)->picBounds.left);
-
- if (maxVValue < 0) maxVValue = 0;
- if (maxHValue < 0) maxHValue = 0;
-
- SetCtlMax((**info).vScrollHand, maxVValue);
- SetCtlMax((**info).hScrollHand, maxHValue);
-
- if (setToZero) {
- SetCtlValue((**info).vScrollHand, 0);
- SetCtlValue((**info).hScrollHand, 0);
- }
- } /* SetScrollMaxima */
-
-
- void SetControlPositions (WindowPtr wp, wiHand info)
- {
- SignedByte oldState;
- short portWidth, portHeight;
- Rect textEditRect, r;
- ControlHandle theControl;
- short contentIndex;
- short buttonLeft;
-
- SetPort(wp);
-
- portWidth = wp->portRect.right - wp->portRect.left;
- portHeight = wp->portRect.bottom - wp->portRect.top;
-
- oldState = HGetState((Handle)info);
- MoveHHi((Handle)info);
- HLock((Handle)info);
-
- HideControl((**info).vScrollHand);
- MoveControl((**info).vScrollHand, portWidth - 15, -1);
- SizeControl((**info).vScrollHand, 16, wp->portRect.bottom - 13);
- ShowControl((**info).vScrollHand);
- r = (**(**info).vScrollHand).contrlRect;
- ValidRect(&r);
-
- HideControl((**info).hScrollHand);
- MoveControl((**info).hScrollHand, -1, wp->portRect.bottom - 15);
- SizeControl((**info).hScrollHand, portWidth - 13, 16);
- ShowControl((**info).hScrollHand);
- r = (**(**info).hScrollHand).contrlRect;
- ValidRect(&r);
-
- SetScrollMaxima(wp, info, FALSE);
- HSetState((Handle)info, oldState);
- } /* SetControlPositions */
-
-
- void NewDisplayWindow ()
- {
- WindowPtr newWindow;
- wiHand info;
- Rect controlRect;
- Str255 title;
- Ptr storage = NewPtrClear(sizeof(WindowRecord));
- Point winTopLeft = {0, 0};
-
- newWindow = GetNewCWindow(kDisplayWindow, storage, (WindowPtr)-1);
- if (newWindow != NIL) {
- SetPort(newWindow);
- info = (wiHand)NewHandleClear(sizeof(WindowInfo));
- SetWRefCon(newWindow, (long)info);
- HLock((Handle)info);
-
- SetRect(&controlRect, 0, 0, 16, 16);
- (**info).vScrollHand = NewControl(newWindow, &controlRect, NIL, FALSE, 0, 0, 0, scrollBarProc, -2);
- (**info).hScrollHand = NewControl(newWindow, &controlRect, NIL, FALSE, 0, 0, 0, scrollBarProc, -2);
- SetControlPositions(newWindow, info);
-
- PrOpen();
- (**info).printInfo = (THPrint)NewHandleClear(sizeof(TPrint));
- PrintDefault((**info).printInfo);
- PrClose();
-
- HUnlock((Handle)info);
-
- /* Next, we have some tricky code to stagger windows. Basically, if the frontmost */
- /* window is a document window, set the new window to start 10 pixels to the right*/
- /* and 40 pixels below the frontmost window. Otherwise, put the window at (10, 40)*/
- if ((FrontWindow() != 0L) && (((WindowPeek)FrontWindow())->windowKind == userKind))
- winTopLeft = topLeft((**((WindowPeek)FrontWindow())->strucRgn).rgnBBox);
- if (!PtInRect(winTopLeft, &(**GetGrayRgn()).rgnBBox))
- SetPt(&winTopLeft, 0, 0);
- winTopLeft.h += 10;
- winTopLeft.v += 40;
- MoveWindow(newWindow, winTopLeft.h, winTopLeft.v, FALSE);
-
- ShowWindow(newWindow);
- }
- } /* NewDisplayWindow */
-
-
- void CloseAWindow (WindowPtr whichWindow)
- {
- short wKind;
- wiHand info;
-
- if (whichWindow) {
- wKind = ((WindowPeek)whichWindow)->windowKind;
- if (wKind < 0)
- CloseDeskAcc(wKind);
- else if (wKind == userKind) {
- info = (wiHand)GetWRefCon(whichWindow);
- DisposeWindow(whichWindow);
- AdjustMenus;
- }
- }
- } /* CloseAWindow */
-
-
- /*----------------------- Handle window update events ----------------------*/
-
-
- void DoUpdate (WindowPtr wp)
- {
- Rect updateRect;
- wiHand info = (wiHand)GetWRefCon(wp);
- Rect portRect = wp->portRect;
- Rect picRect;
- RgnHandle oldClip = nil;
- Rect newClip;
-
- SetPort(wp); /* make update window active grafPort */
- BeginUpdate(wp); /* visRgn temporarily = updateRgn */
- EraseRect(&portRect);
-
- oldClip = NewRgn();
- GetClip(oldClip);
- newClip = portRect; newClip.bottom -= 15; newClip.right -= 15;
- ClipRect(&newClip); /* restrict drawing to the content area of our window */
-
- if ((*info)->thePicture != NIL) { /* If we have a picture, draw it */
- picRect = (*info)->picBounds;
- OffsetRect(&picRect, -(**info).currHScrollValue, -(**info).currVScrollValue); /* scroll into place */
- DrawPicture((*info)->thePicture, &picRect);
- } else
- FillRect(&newClip, ltGray);
- SetOrigin(0, 0);
-
- SetClip(oldClip);
- DisposeRgn(oldClip);
-
- DrawControls(wp);
- DrawGrowIcon(wp);
- EndUpdate(wp); /* restore normal visRgn of grafport */
- } /* DoUpdate */
-
-
- void SetCtlActivate (WindowPtr wp, Boolean activate)
- {
- short hiliteValue;
- ControlHandle currControl;
-
- hiliteValue = activate ? 0 : 255;
- currControl = ((WindowPeek)wp)->controlList;
- while (currControl) {
- HiliteControl(currControl, hiliteValue);
- currControl = (**currControl).nextControl;
- }
- } /* SetCtlActivate */
-
-
- void DoActivate (EventRecord *theEvent)
- {
- wiHand info;
- WindowPtr wp = (WindowPtr)theEvent->message;
-
- AdjustMenus();
- SetPort(wp);
- DrawGrowIcon(wp);
- if (isUserWindow(wp)) {
- info = (wiHand)GetWRefCon(wp);
- SetCtlActivate(wp, theEvent->modifiers & 0x0001);
- }
- } /* DoActivate */
-
-
-
- void ScrollWindow (WindowPtr wp, wiHand info)
- {
- short vScrollAmount, hScrollAmount;
- short vScrollValue = GetCtlValue((**info).vScrollHand), /* We use "get" to clip the control value to between 0 and max */
- hScrollValue = GetCtlValue((**info).hScrollHand);
- Rect scrollArea = wp->portRect;
- RgnHandle scrolledRgn = NewRgn();
-
- SetPort(wp);
- scrollArea.right -= 15; scrollArea.bottom -= 15;
-
-
- vScrollAmount = (**info).currVScrollValue - vScrollValue;
- hScrollAmount = (**info).currHScrollValue - hScrollValue;
-
- (**info).currVScrollValue = vScrollValue; /* Make sure our copy of the scrolling info is up to date */
- (**info).currHScrollValue = hScrollValue;
-
- ScrollRect(&scrollArea, hScrollAmount, vScrollAmount, scrolledRgn);
- InvalRgn(scrolledRgn);
- DisposeRgn(scrolledRgn);
- DoUpdate(wp);
- } /* ScrollWindow */
-
-
-
- pascal void ScrollCallback (ControlHandle whichControl, short partCode)
- {
- short oldValue = GetCtlValue(whichControl);
- short newValue = 0;
- WindowPtr wp = (**whichControl).contrlOwner;
- Boolean vScroll, hScroll;
- Rect portRect = wp->portRect;
- Point contentSize;
- wiHand info;
-
- SetPt(&contentSize, portRect.bottom - portRect.top - 15,
- portRect.right - portRect.left - 15);
- info = (wiHand)GetWRefCon(wp);
- oldValue = GetCtlValue(whichControl);
- vScroll = (whichControl == (**info).vScrollHand);
- hScroll = (whichControl == (**info).hScrollHand);
-
- newValue = oldValue;
- switch (partCode) {
- case 0:
- case inThumb:
- /* newValue = oldValue, as above */
- break;
-
- case inUpButton: /* Arrows scroll 20% */
- if (vScroll)
- newValue -= contentSize.v / 20;
- else
- newValue -= contentSize.h / 20;
- break;
-
- case inDownButton:
- if (vScroll)
- newValue += contentSize.v / 20;
- else
- newValue += contentSize.h / 20;
- break;
-
- case inPageUp: /* Gray areas scroll the window by 80% */
- if (vScroll)
- newValue -= (contentSize.v * 4) / 5;
- else
- newValue -= (contentSize.h * 4) / 5;
- break;
-
- case inPageDown:
- if (vScroll)
- newValue += (contentSize.v * 4) / 5;
- else
- newValue += (contentSize.h * 4) / 5;
- }
-
- /* If the control is "pinned", don't let it go any farther */
- if ((oldValue == 0) && (newValue <= 0)) return;
- if ((oldValue == GetCtlMax(whichControl)) && (newValue >= oldValue)) return;
-
- SetCtlValue(whichControl, newValue);
- ScrollWindow(wp, info);
- } /* ScrollCallback */
-
-
- void DoContentClick (WindowPtr whichWindow, Point globalPt)
- {
- Point localPt;
- ControlHandle whichControl;
- short part;
- wiHand info;
-
- if (whichWindow != FrontWindow())
- SelectWindow(whichWindow);
- else {
- SetPort(whichWindow);
- localPt = globalPt;
- GlobalToLocal(&localPt);
- info = (wiHand)GetWRefCon(whichWindow);
- part = FindControl(localPt, whichWindow, &whichControl);
- if (part != 0) {
- if (part == inThumb) {
- part = TrackControl(whichControl, localPt, NIL);
- ScrollWindow(whichWindow, info);
- } else
- part = TrackControl(whichControl, localPt, (ProcPtr)ScrollCallback);
- } else
- SysBeep(5); /* No clicking on the picture, please */
- }
- } /* DoContentClick */
-
-
- void DoGrowWindow(WindowPtr whichWindow, EventRecord *theEvent)
- {
- Rect sizeRect;
- long newSize;
- wiHand info = (wiHand)GetWRefCon(whichWindow);
- Rect textEditRect;
- short textPanelWidth;
-
-
- sizeRect = screenBits.bounds;
- sizeRect.top = 100;
- sizeRect.left = 100;
- newSize = GrowWindow(whichWindow, theEvent->where, &sizeRect);
- if (newSize != 0) {
- SizeWindow(whichWindow, newSize & 0x0000FFFF, newSize >> 16, TRUE);
- SetPort(whichWindow);
- InvalRect(&whichWindow->portRect);
- SetControlPositions(whichWindow, info);
- }
- }
-